<?php

namespace App\Http\Controllers\zhjyController;

use App\Constant\ApiUrl;
use App\Http\Controllers\Controller;
use Inertia\Inertia;
use Illuminate\Support\Facades\Log;
use App\Models\DawnUser;
use Illuminate\Http\Request;
use App\Services\ZhjyServiceInterface;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Auth;
use App\Utils\Utils;
use App\Pojo\ResponsePojo;
use App\Constant\HttpStatus;
class ZhjyController extends Controller{

    
    private $zhjyServiceInterface;

    public function __construct(ZhjyServiceInterface $zhjyServiceInterface)
    {
        $this->zhjyServiceInterface = $zhjyServiceInterface;
    }

    /**
     * 智慧教育平台登录
     */
    public function zhjyLogin(Request $request){
        $credentials = $request->validate([
            'dawn_zhjyUser' => ['required'],
            'dawn_zhjyPassword' => ['required'],
            'dawn_identity' => ['required'],
            'dawn_classOlder' => ['required'],
        ]);
        
        $result = $this->zhjyServiceInterface->login( $request, $credentials);
        return $result;
    }

    /**
     * 智慧教育平台信息绑定状态查询
     */
    public function findZhjyStatus(){
        $result = $this->zhjyServiceInterface->findZhjyStatus();
        return $result;
    }

    public function log_upload(Request $request){
       
        return Inertia::render('home/index/logUpload/Index');
    }

    public function getZhjyToken(Request $request){
        $result =   $this->zhjyServiceInterface->getZhjyToken();
        Log::info( $result);

    }

    public function home(Request $request){
        $result = $this->zhjyServiceInterface->findZhjyStatus();
        return Inertia::render('home/index/Index', ['result'=>$result]);
    }

    public function bind_info(Request $request){
        $result = $this->zhjyServiceInterface->findZhjyStatus();

        return Inertia::render('home/index/InfoBind/Index', ['result'=>$result]);
    }
    
    public function sign_in(Request $request){
        // $a = $request->all();
        // Log::info('所有数据:');
        // Log::info($a);
        // $data = $request->input('queryDate');
        // Log::info( $data);
        // $queryDate = $data ?? null;
        // if(empty($queryDate)){
        //     $queryDate = Utils::getYMDDate(); // 默认今天
        // }
        // $result = $this->zhjyServiceInterface->getDawnLogs($queryDate);
        // // return Inertia::render('home/index/signIin/Index', ['result'=>$result]);
        return Inertia::render('home/index/signIin/Index');
    }

    public function sign_in_json(Request $request){
        $a = $request->all();
        $data = $request->input('queryDate');
        $queryDate = $data ?? null;
        if(empty($queryDate)){
            $queryDate = Utils::getYMDDate(); // 默认今天
        }
        $result = $this->zhjyServiceInterface->getDawnLogs($queryDate);
        return $result;
    }

    public function uploadFileUrl(Request $request)
    {  

        if ($request->hasHeader('zhjy-type')) {
            // 上传到智慧教育平台
            $result = $this->zhjyServiceInterface->uploadZhjyFileUrl($request);
            Log::info($result);
            $d = is_string($result) ? json_decode($result, true) : $result;
            $d['orgUrl'] = $d['url'];
            $d['url'] = ApiUrl::$baseApiUrl . $d['url']; 

            return ResponsePojo::success(HttpStatus::OK,HttpStatus::getStatusMessage(HttpStatus::OK), $d);
        }
        // 上传到本地
        return $this->zhjyServiceInterface->uploadLocalFileUrl($request);
    }

    public function saveZhjyClock(Request $request){
        $credentials = $request->validate([
            'clockAddress' => ['required'],
            'clockContent' => ['nullable'],
            'filePath' => ['required'],
            'fileIds' => ['required'],
            'orgUrls' => ['required'],
        ]);
        $result = $this->zhjyServiceInterface->saveZhjyClock($credentials);
        return $result;
    }

    public function saveZhjyLog(Request $request){
        $credentials = $request->validate([
            'logType' => ['required'],
            'displayDate' => ['required'],
            'weeklySummary' => ['required'],
            'nextWeekPlan' => ['required'],
            'feedback' => ['nullable'],
            'selfEvaluation' => ['required'],
            'fileList' => ['nullable'],
            'imageList' => ['nullable'],
        ]);
        $result = $this->zhjyServiceInterface->saveZhjyLog($credentials);
        return $result;
    }

    public function getLogRows(Request $request){
        $credentials = $request->validate([
            'pageNum' => ['required'],
            'pageSize' => ['required'],
        ]);
        $result = $this->zhjyServiceInterface->getLogRows($credentials);
        return $result;
    }

    
}